# -*- coding:utf-8 -*-
from urlTestDemo.configUtils import initSysConfInfo

__author__ = u'sangbowei'

import time
from urlTestDemo.shipDB import *
from urlTestDemo.visaDB import *
from urlTestDemo.emailUtils import *
from urlTestDemo.redisUtils import *

from flask import Flask, render_template,redirect,url_for
# from flask_script import Manager
from flask_bootstrap import Bootstrap
from flask_apscheduler import APScheduler

app = Flask(__name__)
# manager = Manager(app)
bootstrap = Bootstrap(app)
scheduler = APScheduler()

class Config(object):
    JOBS = [
        # 样例
        # {
        #     'id': 'job1',
        #     'func': 'jobs:job1',
        #     'args': (1, 2),
        #     'trigger': 'interval',
        #     'seconds': 10
        # }
    ]

# 批量测试 邮轮签证链接,并发送邮件且保存信息至Redis
def daily_test_outbu_url():
    allVisaProblemUrlList = batchTestVisaUrl()  # 存放签证 问题url
    allShipProblemUrlList = batchTestShipUrl()  # 存放邮轮 问题url
    saveProblemUrlListToRedis(allVisaProblemUrlList, allShipProblemUrlList)
    init_problem_url_list(allVisaProblemUrlList, allShipProblemUrlList)
    base_send_email()

# 初始化 ship visa 数据
def initData():
    saveProblemUrlListToRedis(batchTestVisaUrl(),batchTestShipUrl())

# 测试任务1
def test_task_job(a,b):
    print(str(a),str(b))
    print("定时任务 111 测试",time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

# 测试任务2
def test_task_job_2():
    print("定时任务 2222 测试 22",time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


# 添加所有任务至列表中
@app.route('/addAllJob', methods=['GET','POST'])
def addAllJob():
    print("addAllJob start")
    scheduler.add_job(func=test_task_job, id='test_task_job', args=(1, 2), trigger='interval', seconds=2, replace_existing=True)
    scheduler.add_job(func=test_task_job_2, id='test_task_job_2', trigger='interval', seconds=2, replace_existing=True)
    scheduler.add_job(func=daily_test_outbu_url, id='daily_test_outbu_url', trigger='interval', hours=10, replace_existing=True)
    return 'add all job success!'

# 移除任务
@app.route('/removeJob/<id>')
def removeJob(id):
    print("removeJob start")
    scheduler.delete_job(id)
    return "remove job("+id+") success!"

# 暂停任务
@app.route('/pauseJob/<id>')
def pauseJob(id):
    print("pauseJob start")
    scheduler.pause_job(id)
    return "pause job("+id+") success!"

# 恢复任务
@app.route('/resumeJob/<id>')
def resumeJob(id):
    print("resumeJob start")
    scheduler.resume_job(id)
    return "resume job("+id+") success!"

# 获取所有任务
@app.route('/getAllJob')
def  getAllJob() :
    print("getAllJob start")
    jobs = scheduler.get_jobs()
    jobList = 'job list is [ '
    for job in jobs:
        id = job.id
        jobList+=id+' , '
    jobList+=' ]'
    return "get all jobs success! "+jobList

# 手动刷新数据
@app.route('/refresh_data')
def refreshData():
    initData()
    return redirect(url_for('urlList'))

# index test 界面
@app.route('/')
def index():
    return render_template('index.html')

# 出境全品类链接监控URl
@app.route('/urlList')
def urlList():
    visaUrlList = getVisaProblemUrlListFromRedis();
    shipUrlList = getShipProblemUrlListFromRedis();
    return render_template('outbu_bad_url_set.html', visaUrlList=visaUrlList,shipUrlList=shipUrlList)

if __name__ == '__main__':
    app.config.from_object('config')
    scheduler.init_app(app=app)
    scheduler.start()
    initSysConfInfo()
    app.run(debug=True)
